﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity.Person;
using SchoolLibrary.Utils;

namespace SchoolLibrary.Entity
{
    public class Class : Identity
    {
        public const string FIND_BY_ID = "SELECT * FROM class c WHERE c.id = :id";
        public const string FIND_ALL = "SELECT * FROM class";
        
        public const string NUMBER_COLUMN_NAME = "number";
        public const string LETTER_COLUMN_NAME = "letter";
        public const string CLASS_LEADER_ID_COLUMN_NAME = "class_leader_id";

        public const string SAVE = "INSERT INTO class (number, letter, class_leader_id) " +
            "VALUES(:number, :letter, :classLeaderId)";
        public const string UPDATE = "UPDATE class SET number = :number, letter = :letter, " +
            "class_leader_id = :classLeaderId where id = :id";

        private byte number;
        private string letter;
        private ClassLeader classLeader;

        public byte Number
        {
            get { return number; }
            set { number = value; }
        }

        public string Letter
        {
            get { return letter; }
            set { letter = value; }
        }

        public ClassLeader ClassLeader
        {
            get { return classLeader; }
            set { classLeader = value; }
        }

        public override string ToString()
        {
            return number + StringUtil.SPACE + letter;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj == this)
            {
                return true;
            }

            if (!obj.GetType().Equals(typeof(Class)))
            {
                return false;
            }

            Class currentClass = (Class)obj;

            return number == currentClass.number
                && (letter != null ? letter.Equals(currentClass.letter) : currentClass.letter == null);
        }
    }
}
